<?php

class Base_Controller extends Controller {


    // public $restful = true;

    public function __construct(){

        parent::__construct();

        View::share('sidebar', Navigator::sidebar());

        $class = get_called_class();

        if(!Request::ajax()){
            switch($class) {
                case 'Home_Controller':
                    $this->filter('before', 'nonauth')->except('dashboard');
                    break;             

                case 'Console_Auth_Controller':
                    $this->filter('before', 'auth')->except(array('authenticate','verifyupdate','forgot','logout'));
                    break;
                    
                default:
                    
                    $this->filter('before', 'auth');
                    
                    break;
            }
        }

        View::share('unauthorized', $this->_unauthorize());

    }

    protected function _unauthorize(){

        if(Auth::check()){
            $aclAccess = Console_Acl::where('sur_id','=',Auth::user()->sur_id)
                     ->where('sua_modul','=',URI::segment(2))
                     ->where('sua_controller','=',URI::segment(3))
                     ->where('sua_action','=',URI::segment(4))
                     ->first();

            if(empty($aclAccess) && URI::segment(3) != 'dashboard'){
                return true;
            }else{
                return false;
            }
        }

    }

	/**
	 * Catch-all method for requests that can't be matched.
	 *
	 * @param  string    $method
	 * @param  array     $parameters
	 * @return Response
	 */
	public function __call($method, $parameters)
	{
		return Response::error('404');
	}

}